package com.dragon.concurrent.demo.future;

import java.util.function.Consumer;

public class FutureService {


    public <T> Future<T> submit(FutureTask<T> futureTask) {
        AsyncFuture<T> future = new AsyncFuture<>();
        new Thread(() -> {
            T result = null;
            try {
                result = futureTask.call();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            future.done(result);
        }).start();

        return future;
    }

    public <T> Future<T> submit(FutureTask<T> futureTask, Consumer<T> consumer) {
        AsyncFuture<T> future = new AsyncFuture<>();
        new Thread(() -> {
            T result = null;
            try {
                result = futureTask.call();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            future.done(result);
            consumer.accept(result);
        }).start();

        return future;
    }
}